const { ccclass, property } = cc._decorator;

import EventMgr from "./EventMgr";
import MatchGameEventType from "./MatchGameEventType";
import frameworkManager from "./frameworkManager";
import MatchGameData from "./MatchGameData";
import MatchGameConfig from "./MatchGameConfig";
import ObstacleConfig, { LayerType } from "./ObstacleConfig";
import MatchGameDataMgr from "./MatchGameDataMgr";
import SquareStatusMgr from "./SquareStatusMgr";
import SquareItem from "./SquareItem";
import propPrefab from "./propPrefab";
import PropAniCtrl from "./PropAniCtrl";
import { GuideElimitateType } from "./MatchGameGuidePageCtrl";
import MatchGameCenterCtrl from "./MatchGameCenterCtrl";

@ccclass
export default class PropPrefabCtrl extends SquareItem {
    _startPos: cc.Vec2 = null;
    _moveOff: boolean = false;
    haveUsed: boolean = false;
    createdByLightBall: boolean = false;
    duringCreate: boolean = false;

    public ui;

    onLoad() {
        this.onUILoad();
        this.addButtonListen();
    }

    onUILoad() {
        this.ui = this.node.addComponent(propPrefab);
    }

    addButtonListen() {}

    initData() {}

    init(t, a, o) {
        super.init(t, a, MatchGameConfig.SquareType.PropItem, o, null);
        this.unscheduleAllCallbacks();
        this.layerType = LayerType.Middle;
        this.haveUsed = false;
        this._couldTouch = true;
        this.createdByLightBall = false;
        this.duringCreate = true;
        this.ui.spine.getComponent(sp.Skeleton).skeletonData = null;
        this.initAni(o);
        this.node.on(cc.Node.EventType.TOUCH_START, this.touchStartEvent, this);
        this.node.on(cc.Node.EventType.TOUCH_MOVE, this.touchMoveEvent, this);
        this.node.on(cc.Node.EventType.TOUCH_CANCEL, this.touchEndEvent, this);
        this.node.on(cc.Node.EventType.TOUCH_END, this.touchEndEvent, this);
    }

    initAni(e) {
        const t = this;
        PropAniCtrl.playOnePropAppearAni(e, this.ui.spine, () => {
            t.duringCreate = false;
        });
        this.scheduleOnce(() => {
            t.duringCreate = false;
        }, 0.5);
    }

    touchStartEvent(e?) {
        if (
            SquareStatusMgr.getSquareNormalStatus(this) &&
            !MatchGameCenterCtrl._instance.existSquareUpperLayer(this._row, this._col, LayerType.Middle) &&
            this._couldTouch
        ) {
            this._startPos = e.getLocation();
            this._moveOff = true;
            return true;
        }
    }

    touchMoveEvent(e) {
        if (
            !MatchGameCenterCtrl._instance.existSquareUpperLayer(this._row, this._col, LayerType.Middle) &&
            this._couldTouch &&
            this._moveOff &&
            SquareStatusMgr.getSquareNormalStatus(this)
        ) {
            const t = e.getLocation();
            if (this._startPos) {
                const a = Math.abs(t.x - this._startPos.x),
                    o = Math.abs(t.y - this._startPos.y);
                if (MatchGameData.duringGuide) {
                    if (
                        (this.guideElimitateType == GuideElimitateType.Left || 
                        this.guideElimitateType == GuideElimitateType.Any) &&
                        a > o &&
                        a > MatchGameConfig.ITEM_WIDTH / 2 &&
                        t.x - this._startPos.x < 0
                    ) {
                        frameworkManager.log("prop touchMoveEvent", 1);
                        EventMgr.trigger(MatchGameEventType.ON_GUIDE_ELIMITATE);
                        this.setOtherChoose({
                            row: this._row - 1,
                            col: this._col,
                        });
                        return;
                    }
                    if (
                        (this.guideElimitateType == GuideElimitateType.Up || 
                        this.guideElimitateType == GuideElimitateType.Any) &&
                        o > a &&
                        o > MatchGameConfig.ITEM_HEIGHT / 2 &&
                        t.y - this._startPos.y > 0
                    ) {
                        frameworkManager.log("prop touchMoveEvent", 2);
                        EventMgr.trigger(MatchGameEventType.ON_GUIDE_ELIMITATE);
                        this.setOtherChoose({
                            row: this._row,
                            col: this._col - 1,
                        });
                        return;
                    }
                    if (
                        (this.guideElimitateType == GuideElimitateType.Right || 
                        this.guideElimitateType == GuideElimitateType.Any) &&
                        a > o &&
                        a > MatchGameConfig.ITEM_WIDTH / 2 &&
                        t.x - this._startPos.x > 0
                    ) {
                        frameworkManager.log("prop touchMoveEvent", 3);
                        EventMgr.trigger(MatchGameEventType.ON_GUIDE_ELIMITATE);
                        this.setOtherChoose({
                            row: this._row + 1,
                            col: this._col,
                        });
                        return;
                    }
                    if (
                        (this.guideElimitateType == GuideElimitateType.Down || 
                        this.guideElimitateType == GuideElimitateType.Any) &&
                        o > a &&
                        o > MatchGameConfig.ITEM_HEIGHT / 2 &&
                        t.y - this._startPos.y < 0
                    ) {
                        frameworkManager.log("prop touchMoveEvent", 4);
                        EventMgr.trigger(MatchGameEventType.ON_GUIDE_ELIMITATE);
                        this.setOtherChoose({
                            row: this._row,
                            col: this._col + 1,
                        });
                        return;
                    }
                } else if (a > o && a > MatchGameConfig.ITEM_WIDTH / 2 && t.x - this._startPos.x < 0) {
                    frameworkManager.log("prop touchMoveEvent", 11);
                    this.setOtherChoose({
                        row: this._row - 1,
                        col: this._col,
                    });
                } else if (o > a && o > MatchGameConfig.ITEM_HEIGHT / 2 && t.y - this._startPos.y > 0) {
                    frameworkManager.log("prop touchMoveEvent", 22);
                    this.setOtherChoose({
                        row: this._row,
                        col: this._col - 1,
                    });
                } else if (a > o && a > MatchGameConfig.ITEM_WIDTH / 2 && t.x - this._startPos.x > 0) {
                    frameworkManager.log("prop touchMoveEvent", 33);
                    this.setOtherChoose({
                        row: this._row + 1,
                        col: this._col,
                    });
                } else if (o > a && o > MatchGameConfig.ITEM_HEIGHT / 2 && t.y - this._startPos.y < 0) {
                    frameworkManager.log("prop touchMoveEvent", 44);
                    this.setOtherChoose({
                        row: this._row,
                        col: this._col + 1,
                    });
                }
            }
        }
    }

    touchEndEvent() {
        frameworkManager.log("prop touchEnd", this._moveOff);
        if (
            this._moveOff &&
            SquareStatusMgr.getSquareNormalStatus(this) &&
            !(this.itemType == MatchGameConfig.PropType.LightingBall && MatchGameDataMgr.getMaxItem(true) < 0)
        ) {
            this._moveOff = false;
            if (MatchGameData.duringGuide) {
                if (
                    this.guideElimitateType == GuideElimitateType.Click ||
                    this.guideElimitateType == GuideElimitateType.Any
                ) {
                    EventMgr.trigger(MatchGameEventType.ON_GUIDE_ELIMITATE);
                    this.scheduleOnce(() => {
                        MatchGameDataMgr.useOneProp({
                            i: this._row,
                            j: this._col,
                        });
                    }, 0.2);
                    EventMgr.trigger(MatchGameEventType.GUIDE_ELIMINATE);
                }
            } else {
                MatchGameDataMgr.useOneProp({
                    i: this._row,
                    j: this._col,
                });
            }
        }
    }

    public _row;
    public _col;
    public _couldTouch;
    setOtherChoose(e) {
        if (
            this._couldTouch &&
            this._moveOff &&
            !MatchGameCenterCtrl._instance.existSquareUpperLayer(e.row, e.col, LayerType.Middle) && 
            MatchGameDataMgr.middleSquareGrid[e.row][e.col].canMove
        ) {
            if (
                !MatchGameCenterCtrl._instance.displayMapInfo[e.row] ||
                MatchGameCenterCtrl._instance.displayMapInfo[e.row][e.col] !=
                    MatchGameConfig.DisplayMapType.Normal ||
                MatchGameDataMgr.middleSquareGrid[e.row][e.col]
            ) {
                if (
                    MatchGameDataMgr.middleSquareGrid[e.row] &&
                    MatchGameDataMgr.middleSquareGrid[e.row][e.col] &&
                    SquareStatusMgr.getSquareNormalStatus(this) &&
                    SquareStatusMgr.getSquareNormalStatus(MatchGameDataMgr.middleSquareGrid[e.row][e.col])
                ) {
                    this._moveOff = false;
                    if (MatchGameData.duringGuide) {
                        this.scheduleOnce(() => {
                            MatchGameDataMgr.changeProAndOther(
                                {
                                    i: this._row,
                                    j: this._col,
                                },
                                {
                                    i: e.row,
                                    j: e.col,
                                }
                            );
                            EventMgr.trigger(MatchGameEventType.GUIDE_ELIMINATE);
                        }, 0.2);
                    } else {
                        MatchGameDataMgr.changeProAndOther(
                            {
                                i: this._row,
                                j: this._col,
                            },
                            {
                                i: e.row,
                                j: e.col,
                            }
                        );
                    }
                } else {
                    this._moveOff = false;
                    this.touchWall(e);
                }
            } else {
                this._moveOff = false;
                MatchGameDataMgr.changeSquareAndEmpty(
                    {
                        i: this._row,
                        j: this._col,
                    },
                    {
                        i: e.row,
                        j: e.col,
                    }
                );
            }
        }
    }

    touchWall(e) {
        const t = this;
        const a = this.node.getChildByName("spine");
        const o = 5 * -(this._row - e.row);
        const i = 5 * (this._col - e.col);
        const r = 7 * -(this._row - e.row);
        const n = 7 * (this._col - e.col);
        const l = 9 * -(this._row - e.row);
        const c = 9 * (this._col - e.col);
        const u = cc.v3(a.x, a.y);
        const p = cc.v3(a.x + o, a.y + i);
        const d = cc.v3(a.x + r, a.y + n);
        const h = cc.v3(a.x + l, a.y + c);
        const f = 0 == o
            ? {
                  scaleX: 0.93,
                  scaleY: 1.027,
                  position: h,
              }
            : {
                  scaleX: 1.027,
                  scaleY: 0.93,
                  position: h,
              };
              
        frameworkManager.log("touchWall", o, i);
        this.node.zIndex = 999;

        cc.tween(a)
            .to(0.05, {
                scaleX: 0 !== o ? 1 : 1.02,
                scaleY: 0 !== i ? 1 : 1.02,
                position: p,
            })
            .to(0.03, {
                scaleX: 0 !== o ? 1 : 0.958,
                scaleY: 0 !== i ? 1 : 0.958,
                position: d,
            })
            .to(0.05, f)
            .to(0.07, {
                scaleX: 1,
                scaleY: 1,
                position: u,
            }, {
                easing: "backOut",
            })
            .call(() => {
                a.scaleX = 1;
                a.scaleY = 1;
                a.position = u;
                t.node.zIndex = 0;
            })
            .start();
    }

    moveTo(t, a, o) {
        super.moveTo(t, a, o);
    }

    clear() {
        this.node.off(cc.Node.EventType.TOUCH_START, this.touchStartEvent, this);
        this.node.off(cc.Node.EventType.TOUCH_MOVE, this.touchMoveEvent, this);
        this.node.off(cc.Node.EventType.TOUCH_CANCEL, this.touchEndEvent, this);
        this.node.off(cc.Node.EventType.TOUCH_END, this.touchEndEvent, this);
        this.ui.spine.getComponent(sp.Skeleton).skeletonData = null;
    }

    destorySelf() {
        super.destorySelf();
        this.clear();
    }

    playShakeAni() {
        const e = this;
        this.scheduleOnce(() => {
            PropAniCtrl.playOnePropShakeAni(e.itemType, e.ui.spine);
        }, 0.2);
    }

    // update(t) {
    //     super.update(t);
    // }
}
